#  Copyright (c) 2020 - ~, WANGXI
#  
#  CBR E2E DELAY TIME ANALYSYS AWK FILE
#  Used in NS-2 simulator
#  Clone from https://gitee.com/WangXi_Chn/ns2_modulelib
#  Note in https://www.yuque.com/wangxi_chn/kozrfl
#  
#  Change Logs:
#  Date           Author       Notes			Mail
#  2020-12-09     WangXi   	   first version	WangXi_Chn@foxmail.com

BEGIN {
    # 程序初始化，设定一变量以记录目前处理包的最大ID
    highest_packet_id = 0;
}

{
    action = $1; 
    time = $2;
    node_1 = $3;
    node_2 = $4;
    type = $5;
    pktsize = $6;
    flow_id = $8;
    node_1_address = $9;
    node_2_address = $10;
    seq_no = $11;
    packet_id = $12;

    # 记录目前最大的packet ID
    if(packet_id > highest_packet_id){
        highest_packet_id = packet_id;
    }

    # 记录包的传送时间
    if(start_time[packet_id] == 0){
        start_time[packet_id] = time;
    }

    # 记录CBR（flow_id=2）的接收时间
    if(flow_id == 2 && action == "r"){
        end_time[packet_id] = time;
    }
    else{
        # 把不是flow_id=2的包或者是flow_id=2但此包被丢掉的时间设为-1
        end_time[packet_id] = -1;
    }
}

END {
    # 当数据记录全部读取完后，开始计算有效包的端点到端点延迟时间
    for(packet_id = 0;packet_id<=highest_packet_id;packet_id++){
        start = start_time[packet_id];
        end = end_time[packet_id];
        packet_duration = end-start;

        # 只把接收时间大于传送时间的记录列出来
        if(start<end){
            printf("%f %f\n",start,packet_duration);
        }
    }
}

#/ ************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
